/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.standard.unvarying.dataTypes;

import java.text.*;
import java.util.*;

import org.peony.tools.Language;
/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: DisplayType.java, v 0.000 2008-1-11 21:36:43 陈慧然 Exp
 */
public class DisplayDataTypeUtil {
	
	public final static boolean isID(int dataDisplayTypeRef){
		if (dataDisplayTypeRef == DisplayDataTypes.ID || dataDisplayTypeRef == DisplayDataTypes.Table
				|| dataDisplayTypeRef == DisplayDataTypes.TableDir || dataDisplayTypeRef == DisplayDataTypes.Search
				|| dataDisplayTypeRef == DisplayDataTypes.Location || dataDisplayTypeRef == DisplayDataTypes.Locator
				|| dataDisplayTypeRef == DisplayDataTypes.Account || dataDisplayTypeRef == DisplayDataTypes.Assignment
				|| dataDisplayTypeRef == DisplayDataTypes.PAttribute)
			return true;
		return false;
	}
	
	public final static boolean isNumeric(int displayType){
		if (displayType == DisplayDataTypes.Amount || displayType == DisplayDataTypes.Number
				|| displayType == DisplayDataTypes.CostPrice || displayType == DisplayDataTypes.Integer
				|| displayType == DisplayDataTypes.Quantity)
			return true;
		return false;
	}
	
	public final static boolean isText(int displayType) {
		if (displayType == DisplayDataTypes.String || displayType == DisplayDataTypes.Text
				|| displayType == DisplayDataTypes.TextLong || displayType == DisplayDataTypes.Memo)
			return true;
		return false;
	} 
	
	public final static boolean isDate(int displayType) {
		if (displayType == DisplayDataTypes.Date || displayType == DisplayDataTypes.DateTime
				|| displayType == DisplayDataTypes.Time)
			return true;
		return false;
	} 
	
	public final static boolean isLookup(int displayType) {
		if (displayType == DisplayDataTypes.List || displayType == DisplayDataTypes.Table
				|| displayType == DisplayDataTypes.TableDir || displayType == DisplayDataTypes.Search)
			return true;
		return false;
	}
	
	public final static boolean isLOB(int displayType) {
		if (displayType == DisplayDataTypes.Binary || displayType == DisplayDataTypes.TextLong
				|| displayType == DisplayDataTypes.Image)
			return true;
		return false;
	}
	
	
	public final static DecimalFormat getNumberFormat(int displayType,
			Language language) {
		Language myLanguage = language;
		if (myLanguage == null)
			myLanguage = Language.getLoginLanguage();
		Locale locale = myLanguage.getLocale();
		DecimalFormat format = null;
		if (locale != null)
			format = (DecimalFormat) NumberFormat.getNumberInstance(locale);
		else
			format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
		//
		if (displayType == DisplayDataTypes.Integer) {
			format.setParseIntegerOnly(true);
			format.setMaximumIntegerDigits(DisplayDataTypes.INTEGER_DIGITS);
			format.setMaximumFractionDigits(0);
		} else if (displayType == DisplayDataTypes.Quantity) {
			format.setMaximumIntegerDigits(DisplayDataTypes.MAX_DIGITS);
			format.setMaximumFractionDigits(DisplayDataTypes.MAX_FRACTION);
		} else if (displayType == DisplayDataTypes.Amount) {
			format.setMaximumIntegerDigits(DisplayDataTypes.MAX_DIGITS);
			format.setMaximumFractionDigits(DisplayDataTypes.AMOUNT_FRACTION);
			format.setMinimumFractionDigits(DisplayDataTypes.AMOUNT_FRACTION);
		} else if (displayType == DisplayDataTypes.CostPrice) {
			format.setMaximumIntegerDigits(DisplayDataTypes.MAX_DIGITS);
			format.setMaximumFractionDigits(DisplayDataTypes.MAX_FRACTION);
			format.setMinimumFractionDigits(DisplayDataTypes.AMOUNT_FRACTION);
		} else // if (displayType == Number)
		{
			format.setMaximumIntegerDigits(DisplayDataTypes.MAX_DIGITS);
			format.setMaximumFractionDigits(DisplayDataTypes.MAX_FRACTION);
			format.setMinimumFractionDigits(1);
		}
		return format;
	}
	
	
	
	/**
	 * 获取默认精度
	 * @param displayType
	 * @return
	 */
	public final static int getDefaultPrecision(int displayType) {
		if (displayType == DisplayDataTypes.Amount)
			return 2;
		if (displayType == DisplayDataTypes.Number)
			return 6;
		if (displayType == DisplayDataTypes.CostPrice || displayType == DisplayDataTypes.Quantity)
			return 4;
		return 0;
	} 
	
	
}
